package com.orphan.marketBack.controller;

import com.orphan.marketBack.po.GoodsOut;
import com.orphan.marketBack.po.SupplierOut;
import com.orphan.marketBack.po.SupplierOutGoods;
import com.orphan.marketBack.service.GoodsOutService;
import com.orphan.marketBack.service.StockService;
import com.orphan.marketBack.service.SupplierOutService;
import com.orphan.marketBack.util.DateUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SupplierOutController {
    @Autowired
    private SupplierOutService supplierOutService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private StockService stockService;
    @Autowired
    private GoodsOutService goodsOutService;

    @PostMapping("/supplierOut")
    public boolean supplierOut(String goodsInId,String goodsInNum,String goodsInTime,String goodsName,
                           String goodsPrice,String goodsSize,String goodsSupplier,String goodsType){
        String outTime = DateUtil.timeStamp2Date(String.valueOf(System.currentTimeMillis()),null);

        SupplierOut supplierOut = new SupplierOut();
        supplierOut.setSupplierId(goodsInId + "SI");
        supplierOut.setSupplierName(goodsSupplier);
        supplierOut.setSupplierTime(outTime);

        SupplierOutGoods supplierOutGoods = new SupplierOutGoods();
        supplierOutGoods.setGoodName(goodsName);
        supplierOutGoods.setGoodsNum(Integer.parseInt(goodsInNum));
        supplierOutGoods.setSupplierId(goodsInId + "SI");
        supplierOutGoods.setGoodsType(goodsType);
        supplierOutGoods.setGoodsSize(goodsSize);
        supplierOutGoods.setGoodsPrice(goodsPrice);

        boolean b = supplierOutService.insert(supplierOut, supplierOutGoods);
        boolean flag = false;
        if(b){
            com.orphan.util.dto.SupplierOut so = new com.orphan.util.dto.SupplierOut();
            so.setGoodsName(goodsName);
            so.setGoodsNum(goodsInNum);
            so.setGoodsPrice(goodsPrice);
            so.setGoodsSize(goodsSize);
            so.setGoodsType(goodsType);
            so.setSupplierName(goodsSupplier);
            so.setSupplierOutId(goodsInId + "SI");
            so.setSupplierOutTime(outTime);

            rabbitTemplate.convertAndSend("directSupplierOutTwo","supplierOutTwo",so);

            stockService.deleteStock(goodsName);
            GoodsOut goodsOut = new GoodsOut();
            goodsOut.setGoodsInfo("供应商退货");
            goodsOut.setGoodsName(goodsName);
            goodsOut.setGoodsNum(goodsInNum);
            goodsOut.setGoodsOutId(goodsInId + "TH");
            goodsOut.setGoodsTime(outTime);

            flag = goodsOutService.inset(goodsOut);
        }
        return flag;
    }
}
